package cn.darkhorse.admin.controller.basic;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.darkhorse.basic.domain.QmsFeedback;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.exception.ServiceException;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import cn.darkhorse.basic.domain.QmsMoistureTester;
import cn.darkhorse.basic.service.IQmsMoistureTesterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 回潮测试仪Controller
 *
 * @author ytq
 * @date 2024-08-12
 */

@RestController
@RequestMapping("/basic/tester")
public class QmsMoistureTesterController extends BaseController {

    @Resource
    private IQmsMoistureTesterService qmsMoistureTesterService;

    /**
     * 查询回潮测试仪列表
     */
    @ApiOperation("列表")
    @GetMapping("/list")
    public TableDataInfo list(QmsMoistureTester qmsMoistureTester) {
        startPage();
        List<QmsMoistureTester> list = qmsMoistureTesterService.selectQmsMoistureTesterList(qmsMoistureTester);
        return getDataTable(list);
    }


    /**
     * 获取回潮测试仪详细信息
     */
    @ApiOperation("查看")
    @GetMapping(value = "/view")
    public AjaxResult view( Long id) {
        return success(qmsMoistureTesterService.selectQmsMoistureTesterById(id));
    }

    /**
     * 新增回潮测试仪
     */
    @ApiOperation("新增")
    @Log(title = "回潮测试仪", businessType = BusinessType.INSERT)
    @PostMapping(value = "save")
    public AjaxResult add(@RequestBody QmsMoistureTester qmsMoistureTester)
    {
        return toAjax(qmsMoistureTesterService.saveQmsMoistureTester(qmsMoistureTester));
    }

    /**
     * 删除回潮测试仪
     */
    @ApiOperation("删除")
    @Log(title = "回潮测试仪", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(@RequestBody Long id) {
        return toAjax(qmsMoistureTesterService.deleteQmsMoistureTesterById(id));
    }


    /**
     * 批量删除回潮测试仪
     */
            @ApiOperation("批量删除")
    @Log(title = "回潮测试仪", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody Long[] ids) {
        return toAjax(qmsMoistureTesterService.deleteQmsMoistureTesterByIds(ids));
    }

    /**
     * 导出回潮测试仪列表
     */
            @ApiOperation("导出")
    @Log(title = "回潮测试仪", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsMoistureTester qmsMoistureTester) {
        List<QmsMoistureTester> list = qmsMoistureTesterService.selectQmsMoistureTesterList(qmsMoistureTester);
        ExcelUtil<QmsMoistureTester> util = new ExcelUtil<QmsMoistureTester>(QmsMoistureTester.class);
        util.exportExcel(response, list, "回潮测试仪数据");
    }

    /**
     * 导入回潮测试仪列表
     */
    @ApiOperation("导入")
    @Log(title = "回潮测试仪", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<QmsMoistureTester> util = new ExcelUtil<QmsMoistureTester>(QmsMoistureTester.class);
        List<QmsMoistureTester> testerList =  util.importExcel(file.getInputStream());


        if (StringUtils.isNull(testerList) || testerList.isEmpty())
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (QmsMoistureTester qmsMoistureTester : testerList)
        {
            try
            {
                if (StringUtils.length(qmsMoistureTester.getCode())>30){
                    throw new ServiceException("测试仪编号不能超过30个字符");
                }
                if (StringUtils.length(qmsMoistureTester.getModel())>30){
                    throw new ServiceException("测试仪型号不能超过30个字符");
                }
                if ( qmsMoistureTesterService.save(qmsMoistureTester)){
                    successNum++;
                }
            }
            catch (Exception e)
            {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、"+e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return success(successMsg);
    }
    /**
     * 下载回潮测试仪模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<QmsMoistureTester> util = new ExcelUtil<QmsMoistureTester>(QmsMoistureTester.class);
        util.importTemplateExcel(response, "回潮测试仪数据");
    }
}
